﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;

namespace MasterPageGroupF3.AdminPages
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        private MEMBER[] list;
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "Quản lí thành viên";
            
            list = MemberBus.LayBang();
            if (!IsPostBack)
            {
                GridMember.DataSource = list;
                GridMember.DataBind();
            }
        }
        protected void GridMember_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridMember.PageIndex = e.NewPageIndex;
            GridMember.DataBind();
        }
        protected void Add_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ManageMembers_Add.aspx");
        }

        protected void Delete_Click(object sender, ImageClickEventArgs e)
        {
            int flag = 0;
            for (int i = 0; i < GridMember.Rows.Count; i++)
            {
                CheckBox Chk = (CheckBox)GridMember.Rows[i].FindControl("chk1");
                if (Chk != null)
                {
                    if (Chk.Checked == true)
                    {
                        int id = Int32.Parse(GridMember.Rows[i].Cells[1].Text);
                        MemberBus.deleteMember(id);
                        flag = 1;
                    }
                }
            }
            if (flag == 0)
                Response.Write("<script>alert('Bạn phải check vào 1 nhà cung cấp!');</script>");
            else
                Response.Redirect("ManageMembers.aspx");
        }

        protected void Help_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("HelpAdmin.aspx");
        }

        protected void Go_Click(object sender, EventArgs e)
        {
            
            String txt = txtsearch.Text;
            List<MEMBER> l = new List<MEMBER>();
            for (int i = 0; i < list.Length; i++)
                if (list[i].NAME.Contains(txt) || list[i].CMND.Contains(txt) || list[i].PHONE.Contains(txt) || list[i].USERNAME.Contains(txt) || list[i].PASSWORD.Contains(txt) || list[i].ADDRESS.Contains(txt))
                    l.Add(list[i]);
            if (l != null)
            {
                GridMember.DataSource = l;
                GridMember.DataBind();
            }
        }

        protected void btreset_Click(object sender, EventArgs e)
        {
            GridMember.DataSource = list;
            GridMember.DataBind();

        }
    }
}